# Automation Bias Data Analysis

# import required packages
library(tidyverse)
library(dplyr)
library(stringr)
library(ggplot2)
library(MetBrewer)
library(corrplot)
library(stargazer)
library(ellipse)
library(weights)

coolors <- met.brewer("Paquin",n=11,type="continuous")
print(coolors)
coolors[1:11]


#----- Read in data -----#
data_og <- read.csv('automationBias_dataComplete_withIndices.csv')

data_long <- read.csv('reshaped_automationBias_dataComplete_withIndices.csv')


#---------- Create a plot that maps out mean AI switching rate (Figure 3 in paper)

#----- For AI Treatment == 1 
data_for_scatter_plots_AI <- data_long %>%
  select(country, weight, treatment_AI, treatment_Human, treatment_HighConfidence, treatment_LowConfidence, switched, Overall_AISentiment_Index, Normalized_Overall_AIBeliefs2, Overall_AIBeliefs2, AI_Background_Index4) %>%
  filter(treatment_AI == 1)

summary(data_for_scatter_plots_AI)

figure_3 <- ggplot(data=data_for_scatter_plots_AI, mapping = aes(x = AI_Background_Index4^2, y = switched, color = as.factor(treatment_AI), size = weight, weight= weight)) + 
  geom_smooth(aes(fill = as.factor(treatment_AI))) +
  theme_bw() +
  theme(text=element_text(family="Times", size=14)) +
  scale_color_manual(values=coolors[3]) +
  scale_fill_manual(values = coolors[5]) +
  labs(x = "AI Background (squared)", y = "Switched Identification Post-Treatment (AI Algorithm)") +
  theme(legend.position="none") +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)))

#----- Correlation Plots for Demographics (Figure A.1 in the paper)

correlations <- data_long %>%
  select(country, weight, sex, age, level, QPolitical, EducationQ, total_practice_correct, treatment_HighConfidence, treatment_LowConfidence, treatment_AI, treatment_Human, switched, switched_AI, switched_Human, AI_Background_Index4, Overall_AIBeliefs2) %>%
  mutate(QPolitical = ifelse(QPolitical == 999, NA, QPolitical)) %>%
  mutate(EducationQ = ifelse(EducationQ == "I attended university but did not graduate",2,
                             ifelse((EducationQ == "I attended university and achieved a Bachelor’s degree (e.g. BA, BSc, etc.)" | EducationQ == "I attended university and achieved a Bachelor's degree (for example BA, BSc, etc.)"),3,
                                    ifelse((EducationQ == "I attended graduate school but did not graduate" | EducationQ == "I attended university and began a graduate programme, but did not complete that programme"),4,
                                           ifelse((EducationQ == "I attended graduate school and went to achieve a higher degree (e.g. MA, MSc, PhD, Mphil, etc.)" | EducationQ == "I attended university and went on to achieve a higher degree (for example MA, MSc, PhD, Mphil, etc.)"),5,
                                                  ifelse(EducationQ == "I have some qualifications but did not attend university",1, 
                                                         ifelse(EducationQ == "I have no formal qualifications",0,NA))))))) %>%
  mutate(sex = ifelse(sex == "Female", 1,
                      ifelse(sex == "Male", 0, NA))) %>%
  mutate(level = ifelse(level == "level3_test1", 1,
                        ifelse(level == "level3_test2", 2,
                               ifelse(level == "level3_test3", 3,
                                      ifelse(level == "level3_test4", 4,
                                             ifelse(level == "level3_test5", 5,
                                                    ifelse(level == "level4_test1", 6,
                                                           ifelse(level == "level4_test2", 7,
                                                                  ifelse(level == "level4_test3", 8,
                                                                         ifelse(level == "level4_test4", 9,
                                                                                ifelse(level == "level4_test5", 10,NA))))))))))) %>%
  mutate(level_difficulty = ifelse(level <= 5,3,4))


correlations2 = correlations %>%
  select(-country, -level, -treatment_AI, -treatment_HighConfidence, -treatment_LowConfidence, -treatment_Human, -level) %>%
  rename(`Sex (Female = 1)` = "sex", Age = "age",
         `Highest Level of Education` = "EducationQ",
         `Total Number of Practice Rounds Correct` = "total_practice_correct",
         `Switched Identification After Treatment` = "switched",
         `Switched Identification After AI Algorithm Treatment` = "switched_AI",
         `Switched Identification After Human Analyst Treatment` = "switched_Human",
         `AI Background Index` = "AI_Background_Index4",
         `Trust in AI` = "Overall_AIBeliefs2",
         `Level Difficulty` = "level_difficulty",
         `Political Ideology (Right = 10)` = "QPolitical")

plot.new(); dev.off()

M <- cor(correlations2, use = "na.or.complete")

#----- correlation function -----#
cor.mtest <- function(mat, ...) {
  mat <- as.matrix(mat)
  n <- ncol(mat)
  p.mat<- matrix(NA, n, n)
  diag(p.mat) <- 0
  for (i in 1:(n - 1)) {
    for (j in (i + 1):n) {
      tmp <- cor.test(mat[, i], mat[, j], ...)
      p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
    }
  }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
  p.mat
}

#-------------------------------#

pal <- colorRampPalette(c("white",coolors[3]))
plot.new()
dev.off()

# create weighted correlation plot

# dropping any NA rows
correlations3 <- correlations2 %>%
  drop_na()

# need to convert any NAs to 0
correlations4 <- correlations2 %>%
  replace(is.na(.), 0)

weighted_correlations_0 <- cov.wt(correlations4, wt = correlations4$weight, cor = TRUE)$cor

weighted_correlations_0 <- weighted_correlations_0[-1,-1]

weighted_correlations_complete <- cov.wt(correlations3, wt = correlations3$weight, cor = TRUE)$cor

weighted_correlations_complete <- weighted_correlations_complete[-1,-1]


# plot 0 
p.mat0 <- cor.mtest(correlations4)

p.mat0 <- p.mat0[-1,-1]


corrplot(weighted_correlations_0, type="lower", family = "Times",
         col=pal(10), tl.cex=0.75,
         tl.col="black", tl.srt=45,
         p.mat = p.mat0, sig.level = c(.001, .01, .05), insig = "label_sig", pch.col = "black", pch.cex = .75,
         method = "color")

# plot no NA

p.matNoNA <- cor.mtest(correlations3)

p.matNoNA <- p.matNoNA[-1,-1]


corrplot(weighted_correlations_complete, type="lower", family = "Times",
         col=pal(10), tl.cex=0.75,
         tl.col="black", tl.srt=45,
         p.mat = p.matNoNA, sig.level = c(.001, .01, .05), insig = "label_sig", pch.col = "black", pch.cex = .75,
         method = "color")

#----- Summary Statistics (Table 1 in paper) -----#


for_summary_statistics <- correlations %>%
  select(-country,-level, -level_difficulty, -switched_Human, -switched_AI, -weight)

summary(for_summary_statistics)
stargazer(for_summary_statistics, title="Descriptive Statistics", digits=2, out="tableNEW.html", style = "apsr",
          covariate.labels = c("Sex (Female = 1)", "Age", "Political Ideology (Right = 10)", "Highest Level of Education","Total Number of Practice Rounds Correct","Received a High Confidence Treatment",
                              "Received a Low Confidence Treatment", "Received an AI Algorithm Treatment","Received a Human Analyst Treatment","Switched Identification After Treatment",
                               "AI Background Index","Trust in AI"),
          summary.stat = c("n", "mean", "sd", "min","max"))


#----- Mean Times Switched x Treatment Details (Figure 2 in the paper)

data_for_plotting <- data_og %>%
  select(caseid, country, region, weight,
         total_practice_correct, percentage_practice_correct,
         Lvl3_pre_treatment_accuracy,Lvl3_post_treatment_accuracy,Lvl3_times_switched, Lvl3_times_switchedAI, Lvl3_times_switchedHuman,
         Lvl4_pre_treatment_accuracy,Lvl4_post_treatment_accuracy,Lvl4_times_switched, Lvl4_times_switchedAI, Lvl4_times_switchedHuman,
         overall_pre_treatment_accuracy, overall_post_treatment_accuracy, Overall_times_switched, Overall_times_switchedAI, Overall_times_switchedHuman,
         Overall_Treatment_Accuracy,
         FamiliarityAIQ_1, FamiliarityAIQ_2, FamiliarityAIQ_3, FamiliarityAIQ_4, FamiliarityAIQ_5, FamiliarityAIQ_6,
         ProgrammingExperienceQ, 
         AIBeliefsQ_A, AIBeliefsQ_B, AIBeliefsQ_C, AIBeliefsQ_D, AIBeliefsQ_E, AIBeliefsQ_F, AIBeliefsQ_G,
         ApplicationsUseQ_1, ApplicationsUseQ_2, ApplicationsUseQ_3, ApplicationsUseQ_4, ApplicationsUseQ_5, ApplicationsUseQ_6, ApplicationsUseQ_7, ApplicationsUseQ_8, ApplicationsUseQ_9,
         PotentialAIQ_1, PotentialAIQ_2, PotentialAIQ_3, PotentialAIQ_4, PotentialAIQ_5, PotentialAIQ_6, PotentialAIQ_7, PotentialAIQ_8, PotentialAIQ_9,
         overallAI_Knowledge, overallAI_Experience2, overallAI_Familiarity, AI_Background_Index4, Overall_AISentiment_Index,
         sex, age, QPolitical, EducationQ, total_practice_correct)

data_reshape_switching <- data_for_plotting %>%
  select(caseid, weight, country, Overall_times_switchedAI, Overall_times_switchedHuman) %>%
  rename(`Human Analyst` = "Overall_times_switchedHuman", `AI Algorithm` = "Overall_times_switchedAI",
         Country = "country") %>%
  gather(key = "Switching Type", value = "Count", c("Human Analyst":"AI Algorithm")) %>%
  mutate(Count = as.numeric(Count))


data_reshape_switching_weighted <- data_reshape_switching %>%
  group_by(Country, `Switching Type`) %>%
  mutate(mean = mean(Count, na.rm = TRUE)) %>%
  mutate(weighted_mean_count = weighted.mean(Count,weight)) %>%
  mutate(sd_count = sd(Count, na.rm = TRUE)) %>%
  mutate(n_count = n()) %>%
  mutate(se_weighted_mean = sd_count / sqrt(n_count),
         lower_ci_weighted_mean = weighted_mean_count - qt(1 - (0.05 / 2), n_count - 1) * se_weighted_mean,
         upper_ci_weighted_mean = weighted_mean_count + qt(1 - (0.05 / 2), n_count - 1) * se_weighted_mean)

data_reshape_switching_granular <- data_long %>%
  select(caseid, weight, country, level, image, switched_AI, switched_Human, treatment_HighConfidence, treatment_LowConfidence) %>%
  rename(`Human Analyst` = "switched_Human", `AI Algorithm` = "switched_AI",
         Country = "country") %>%
  mutate(`Treatment Confidence` = ifelse(treatment_HighConfidence == 1, "High", "Low")) %>%
  select(-treatment_HighConfidence, -treatment_LowConfidence) %>%
  gather(key = "Switching Type", value = "Count", c("AI Algorithm":"Human Analyst"))


data_reshape_switching_granular_weighted_altogether <- data_reshape_switching_granular %>%
  select(caseid, weight, `Switching Type`,`Treatment Confidence`, Count) %>%
  group_by(`Switching Type`,`Treatment Confidence`) %>%
  mutate(mean = mean(Count, na.rm = TRUE)) %>%
  mutate(weighted_mean_count = weighted.mean(Count,weight,na.rm = TRUE)) %>%
  mutate(sd_count = sd(Count, na.rm = TRUE)) %>%
  mutate(n_count = n()) %>%
  mutate(se_weighted_mean = sd_count / sqrt(n_count),
         lower_ci_weighted_mean = weighted_mean_count - qt(1 - (0.05 / 2), n_count - 1) * se_weighted_mean,
         upper_ci_weighted_mean = weighted_mean_count + qt(1 - (0.05 / 2), n_count - 1) * se_weighted_mean) %>%
  select(-caseid,-weight, -Count) %>%
  distinct() %>%
  arrange(weighted_mean_count)

data_reshape_switching_granular_weighted_altogether$index <- 1:nrow(data_reshape_switching_granular_weighted_altogether)

plot_switching_granular_weighted_altogether <- ggplot(data_reshape_switching_granular_weighted_altogether, aes(x = index, y = weighted_mean_count,
                                                                                                               color = `Switching Type`, shape = `Treatment Confidence`)) + 
  geom_point(position = position_dodge(width = .7), size = 2) +
  geom_errorbar(aes(ymin=lower_ci_weighted_mean, ymax=upper_ci_weighted_mean), width=0, position = position_dodge(width = .7)) +
  labs(y = "Mean Times Switched", x = "") +
  theme_bw() +
  coord_flip() +
  scale_color_manual(values=c(coolors[3], coolors[9])) +
  theme(text= element_text(family="Times"), axis.ticks.y=element_blank(), axis.text.y = element_blank()) +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0)))

#----- Mean Level AI Indicators (Figure A.3 in paper)
data_reshape_background <- data_for_plotting %>%
  select(caseid, country, weight, overallAI_Knowledge, overallAI_Experience2, overallAI_Familiarity, AI_Background_Index4) %>%
  rename(`AI Knowledge` = "overallAI_Knowledge", `AI Experience` = "overallAI_Experience2", `AI Familiarity` = "overallAI_Familiarity",
         `Overall AI Background` = "AI_Background_Index4", Country = "country") %>%
  gather(key = "Indicator", value = "Level", c("AI Knowledge":"Overall AI Background"))

data_background_weighted_altogether <- data_reshape_background %>%
  select(-Country) %>%
  group_by(Indicator) %>%
  mutate(mean = mean(Level, na.rm = TRUE)) %>%
  mutate(weighted_mean_level = weighted.mean(Level,weight,na.rm = TRUE)) %>%
  mutate(sd_level = sd(Level, na.rm = TRUE)) %>%
  mutate(n_level = n()) %>%
  mutate(se_weighted_mean = sd_level / sqrt(n_level),
         lower_ci_weighted_mean = weighted_mean_level - qt(1 - (0.05 / 2), n_level - 1) * se_weighted_mean,
         upper_ci_weighted_mean = weighted_mean_level + qt(1 - (0.05 / 2), n_level - 1) * se_weighted_mean) %>%
  select(-caseid, -weight, -Level) %>%
  distinct() %>%
  arrange(weighted_mean_level)

data_background_weighted_altogether$index <- 1:nrow(data_background_weighted_altogether)


plot_AI_background_weighted_altogether <- ggplot(data_background_weighted_altogether, aes(x = index, y = weighted_mean_level,
                                                                                          color = `Indicator`, shape = `Indicator`)) + 
  geom_point(position = position_dodge(width = .7)) +
  geom_errorbar(aes(ymin=lower_ci_weighted_mean, ymax=upper_ci_weighted_mean), width=0, position = position_dodge(width = .7)) +
  labs(y = "Mean Level", x = "") +
  theme_bw() +
  coord_flip() +
  scale_color_manual(values=c(coolors[2],coolors[4],coolors[8],coolors[10])) +
  theme(text= element_text(family="Times"), axis.ticks.y=element_blank(), axis.text.y = element_blank()) +
  scale_y_continuous(breaks = c(0.10,0.15,0.20,0.25))

#----- Likert Charts for Certain Text Responses (Figure 5 in paper) -----#

#----------AI Beliefs

beliefs_text <- data_for_plotting %>%
  select(caseid, country, weight, AIBeliefsQ_A, AIBeliefsQ_B, AIBeliefsQ_C, AIBeliefsQ_D, AIBeliefsQ_E, AIBeliefsQ_F, AIBeliefsQ_G) %>%
  rename(`Organizations use artificial intelligence unethically.` = "AIBeliefsQ_A",
         `I am interested in using artificially intelligent systems in my daily life.` = "AIBeliefsQ_B",
         `I find artificial intelligence sinister.` = "AIBeliefsQ_C",
         `I think artificial intelligence is dangerous.` = "AIBeliefsQ_D",
         `Artificial intelligence is exciting.` = "AIBeliefsQ_E",
         `I think artificial intelligence, on balance, is more accurate than humans.` = "AIBeliefsQ_F",
         `There are many beneficial applications of artificial intelligence.` = "AIBeliefsQ_G") %>%
  gather(key = "Statement", value = "Response", c("Organizations use artificial intelligence unethically.":"There are many beneficial applications of artificial intelligence.")) %>%
  mutate(Response = ifelse(Response == "Don’t know" | Response == "Don't know","I don't know",Response)) %>%
  filter(Response != "I don't know") %>%
  group_by(Statement, Response) %>%
  summarise(weighted_n = sum(weight)) %>%
  group_by(Statement) %>%
  mutate(weighted_group_size = sum(weighted_n), 
         weighted_estimate = weighted_n / weighted_group_size)

#-----Reshapes Data
beliefs_text2 <- beliefs_text %>%
  select(Statement, Response, weighted_estimate) %>%
  spread(key = Response, value = weighted_estimate)

beliefs_text3 <- beliefs_text2 %>%
  mutate(No_op_left = (`Neither Agree nor Disagree`/ 2)) %>%
  mutate(No_op_right = `Neither Agree nor Disagree`/ 2) %>%
  select(Statement,`Strongly Disagree`,`Somewhat Disagree`,`No_op_left`,`No_op_right`,`Somewhat Agree`,`Strongly Agree`) %>%
  mutate(No_op_left = -100*No_op_left) %>%
  mutate(`Somewhat Disagree` = -100*`Somewhat Disagree`) %>%
  mutate(`Strongly Disagree` = -100*`Strongly Disagree`) %>%
  mutate(`Somewhat Agree` = 100*`Somewhat Agree`) %>%
  mutate(`Strongly Agree` = 100*`Strongly Agree`) %>%
  mutate(`No_op_right` = 100*`No_op_right`)

beliefs_netperception <- beliefs_text3 %>%
  mutate(netperception = `Somewhat Agree`+`Strongly Agree`+`Strongly Disagree`+`Somewhat Disagree`) %>%
  mutate(netperception = round(netperception,2)) %>%
  mutate(percentageagree = `Somewhat Agree`+`Strongly Agree`) %>%
  mutate(percentagedisagree = -1*(`Strongly Disagree`)+(-1*(`Somewhat Disagree`))) %>%
  mutate(percentagedisagree = round(percentagedisagree,2)) %>%
  mutate(percentageagree = round(percentageagree,2))

x <- c("Strongly Disagree", "Somewhat Disagree", "No_op_left", "Strongly Agree", "Somewhat Agree","No_op_right")

beliefs_text4 <- beliefs_text3 %>%
  gather(key=Response,value=weighted_estimate,c("Strongly Disagree":"Strongly Agree")) %>%
  group_by(Statement) %>%
  mutate(Response = factor(Response, levels = x)) %>%
  mutate(Statement =  factor(Statement, levels = c("Artificial intelligence is exciting.", "I am interested in using artificially intelligent systems in my daily life.", "I find artificial intelligence sinister.", "I think artificial intelligence is dangerous.","I think artificial intelligence, on balance, is more accurate than humans.", "Organizations use artificial intelligence unethically.", "There are many beneficial applications of artificial intelligence."))) %>%
  arrange(Statement) 


plot_AI_Beliefs <- ggplot() +
  geom_bar(stat = "identity", aes(x = Statement, y = weighted_estimate, fill = Response), data = beliefs_text4) +
  coord_flip() +
  theme_minimal() +
  theme(
    text = element_text(family = "Times", size=16),
    panel.grid.major.y = element_line(colour = "grey", size = 0.25),
    panel.grid.minor = element_blank(),
    panel.grid.major.x = element_blank(),
    plot.title = element_text(hjust = 0.5),
    legend.title = element_text(size = 8)
  ) +
  scale_fill_manual(
    breaks = c("Strongly Disagree", "Somewhat Disagree", "No_op_left", "Strongly Agree", "Somewhat Agree"),
    values = c("Strongly Disagree"=coolors[2], "Somewhat Disagree"=coolors[3], "No_op_left"=coolors[6], "Strongly Agree"=coolors[8], "Somewhat Agree"=coolors[9], "No_op_right"=coolors[6]),
    labels = c("Strongly Disagree", "Somewhat Disagree", "Neither Agree nor Disagree", "Somewhat Agree", "Strongly Agree")
  ) +
  labs(fill = " ", y = "Percentage", x = " ") +
  ylim(-100, 100) +
  ggtitle("") +
  geom_hline(yintercept = 0, color = "grey", size = 0.25) +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0)))

#----- Rate of Switching for AI vs Trust in AI vs Treatment Confidence (Figure 6 in paper) -----#

figure_6 <- ggplot(data=data_for_scatter_plots_AI, mapping = aes(x = Normalized_Overall_AIBeliefs2^2, y = switched, color = as.factor(treatment_HighConfidence), size = weight, weight= weight)) + 
  geom_smooth(aes(fill = as.factor(treatment_HighConfidence))) +
  theme_bw() +
  theme(text= element_text(family="Times", size=14))   +
  scale_color_manual(values= c(coolors[3], coolors[9]), labels=c("Low Confidence", "High Confidence")) +
  scale_fill_manual(values = c(coolors[5], coolors[7]), labels=c("Low Confidence", "High Confidence")) +
  labs(x = "Trust in AI (squared)", y = "Switched Identification Post-Treatment (AI Algorithm)", color = "", fill = "") +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)))


#----- Plotting Self-Confidence vs Treatment Confidence (Figure 7 in paper) -----#

data_for_scatter_plots_AI_Confidence <- data_long %>%
  select(country, weight, treatment_AI, treatment_Human, treatment_HighConfidence, treatment_LowConfidence, switched, Overall_AISentiment_Index, percentage_practice_correct, total_practice_correct) %>%
  filter(treatment_AI == 1)

#----- Figure 7A in paper

figure_7a <- ggplot(data=data_for_scatter_plots_AI_Confidence, mapping = aes(x = percentage_practice_correct, y = switched, color = as.factor(treatment_HighConfidence), size = weight, weight= weight)) + 
  geom_smooth(aes(fill = as.factor(treatment_HighConfidence)), method="gam", formula = y ~ s(x, bs = "cs", k=5)) +
  theme_bw() +
  theme(text= element_text(family="Times", size=14))  +
  scale_color_manual(values= c(coolors[3], coolors[9]), labels=c("Low Confidence", "High Confidence")) +
  scale_fill_manual(values = c(coolors[5], coolors[7]), labels=c("Low Confidence", "High Confidence")) +
  labs(x = "Practice Round Accuracy", y = "Switched Identification Post-Treatment (AI Algorithm)", color = "", fill = "") +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)))


data_for_scatter_plots_Human_Confidence <- data_long %>%
  select(country, weight, treatment_AI, treatment_Human, treatment_HighConfidence, treatment_LowConfidence, switched, Overall_AISentiment_Index, percentage_practice_correct, total_practice_correct) %>%
  filter(treatment_Human == 1)

#----- Figure 7B in paper

figure_7b <- ggplot(data=data_for_scatter_plots_Human_Confidence, mapping = aes(x = percentage_practice_correct, y = switched, color = as.factor(treatment_HighConfidence), size = weight, weight= weight)) + 
  geom_smooth(aes(fill = as.factor(treatment_HighConfidence)), method="gam", formula = y ~ s(x, bs = "cs", k=5)) +
  theme_bw() +
  theme(text= element_text(family="Times", size=14))  +
  scale_color_manual(values= c(coolors[3], coolors[9]), labels=c("Low Confidence", "High Confidence")) +
  scale_fill_manual(values = c(coolors[5], coolors[7]), labels=c("Low Confidence", "High Confidence")) +
  labs(x = "Practice Round Accuracy", y = "Switched Identification Post-Treatment (Human Analyst)", color = "", fill = "") +
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)))

#----- Calculating Accuracy of Respondents based on # practice rounds correct and treatment type (Figure A.2 in paper) -----#

data_for_accuracy <- data_long %>%
  select(country, weight, total_practice_correct, treatment_AI, treatment_Human, treatment_HighConfidence, treatment_LowConfidence, treatment_suggestion_correct, identification_correct, confirmation_correct, switched, Overall_AISentiment_Index, Normalized_Overall_AIBeliefs2, Overall_AIBeliefs2, AI_Background_Index4)


# if the original was wrong, treatment was correct, and switched, incorrect -> correct answer (using treatment GOOD = 100)

# if the original was correct, treatment was wrong, and switched, incorrect -> correct answer (using treatment BAD = 0)

# other scenarios: NA

data_for_accuracy1 <- data_for_accuracy %>%
  mutate(using_treatment_correctly = ifelse(identification_correct == 0 & treatment_suggestion_correct == 1 & switched == 1,100,
                                            ifelse(identification_correct == 1 & treatment_suggestion_correct == 0 & switched == 1, 0,NA))) %>%
  # break up practice correct into binary
  mutate(self_confidence_level = ifelse(total_practice_correct >= 3,1,0)) %>%
  group_by(AI_Background_Index4,treatment_AI,self_confidence_level) %>%
  summarise(weighted_accuracy = weighted.mean(using_treatment_correctly,weight,na.rm=TRUE)) %>%
  mutate(line = ifelse(self_confidence_level == 0 & treatment_AI == 0,"Low Self Confidence; Human Treatment",
                       ifelse(self_confidence_level == 1 & treatment_AI == 0, "High Self Confidence; Human Treatment",
                              ifelse(self_confidence_level == 0 & treatment_AI == 1, "Low Self Confidence; AI Treatment",
                                     ifelse(self_confidence_level == 1 & treatment_AI == 1, "High Self Confidence; AI Treatment",NA)))))


figure_a2 <- ggplot(data = data_for_accuracy1, mapping = aes(x = AI_Background_Index4, y = weighted_accuracy, color = as.factor(line))) +
  geom_smooth(aes(fill = as.factor(line)), alpha=0.2, method="gam", formula = y ~ s(x, bs = "cs", k=5)) +
  theme_bw() +
  theme(text= element_text(family="Times"))  +
  scale_color_manual(values= c(coolors[2], coolors[5], coolors[7], coolors[9])) +
  scale_fill_manual(values = c(coolors[2], coolors[5], coolors[7], coolors[9])) +
  labs(y = "Accurate Use of Decision Aid", x = "AI Background Index", color = "", fill = "") 



